import { exit, cd } from 'shelljs';
import { existsSync, mkdirSync } from 'fs';
import { printMsg } from './common';
import { red } from 'chalk';
import { prompt } from 'inquirer';
import { FeatureProject } from '../feature/FeatureProject';
import { FeatureList } from '../feature/FeatureList';

export function createProjectFile (pName: string) {
  // 判断文件夹是否存在
  if (existsSync(pName)) {
    printMsg(red(`${pName}文件夹已经存在`));
    // 退出
    exit(1);
  }
  // 创建文件夹
  mkdirSync(pName);
}

type PType = {
  ptype: string;
}
type FeatureType = {
  feature: Array<string>;
}

export async function selectFeature ():Promise<{projectType:PType, featureList:FeatureType}> {
  const projectType = await prompt([
    {
      type: 'list',
      message: '请选择项目:',
      name: 'ptype',
      choices: [
        {
          name: 'vue',
          value: 'vue',
          checked: true,
          disabled: false, // 是都禁用
          description: 'vue' // 描述
        },
        {
          name: 'react',
          value: 'react',
          checked: false,
          disabled: false,
          description: 'react'
        },
        {
          name: 'exprerss',
          value: 'exprerss',
          checked: false,
          disabled: false,
          description: 'exprerss'
        }
      ]
    }]);
  const featureList = await prompt([
    {
      type: 'checkbox',
      message: '请选择实现的功能:',
      name: 'feature',
      choices: [
        {
          name: 'eslint',
          value: 'eslint',
          checked: true,
          disabled: false, // 是都禁用
          description: 'eslint' // 描述
        },
        'commitizen',
        'husky',
        {
          name: 'typescript',
          value: 'typescript',
          checked: true, // 默认是选中的
          disabled: false,
          description: 'typescript'
        }
      ]
    }
  ]);
  return { projectType, featureList };
}

export function initFeature (pName: string, projectType:string, featureList: Array<string>) {
  // 1. cd 项目目录 并且 初始化package.json
  cd(pName);
  // exec('npm init -y');
  // 2. 根据feature 实现对应的功能
  // vue react express
  // featureList exlint typescript commitizen husky
  // 创建一个类 参数 : 根据项目类型创建项目
  new FeatureProject(projectType);
  // 根据功能实现对应的方法
  new FeatureList(projectType, featureList);
}
